<template>
  <div>
    <el-upload
      action="#"
      list-type="picture-card"
      :auto-upload="false"
      :fileList="fileLists"
      :before-upload="beforeAvatarUpload"
    >
      <i slot="default" class="el-icon-plus"></i>
      <div slot="file" slot-scope="{ file }">
        <img class="el-upload-list__item-thumbnail" :src="file.url" alt="" />
        <span class="el-upload-list__item-actions">
          <span
            class="el-upload-list__item-preview"
            @click="handlePictureCardPreview(file)"
          >
            <i class="el-icon-zoom-in"></i>
          </span>

          <span
            v-if="!disabled"
            class="el-upload-list__item-delete"
            @click="handleRemove(file)"
          >
            <i class="el-icon-delete"></i>
          </span>
        </span>
      </div>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <!-- <el-image style="width: 100%" :src="dialogImageUrl"> </el-image> -->
    </el-dialog>
    <ElImageViewer
      v-if="showViewer"
      :on-close="closeViewer"
      :url-list="fileLists"
    />
  </div>
</template>
<script>
import ElImageViewer from "element-ui/packages/image/src/image-viewer";

export default {
  // eslint-disable-next-line vue/no-unused-components
  components: { ElImageViewer },
  props: {
    fileList: {
      type: Array,
      require: true,
      default: () => [],
    },
  },
  data() {
    return {
      dialogImageUrl: "",
      dialogVisible: false,
      disabled: false,
      fileLists: this.fileList,
      showViewer: false,
      srcList: [],
    };
  },
  watch: {
    fileList: {
      handler(val) {
        if (val && val.length) {
          console.log(val, "val");
          this.srcList = val.map((item) => {
            return item.url;
          });
        } else {
          this.srcList = [];
        }
      },
      deep: true,
      immediate: true,
    },
  },
  methods: {
    closeViewer() {
      this.showViewer = false;
    },
    handleRemove(file) {
      console.log(file);
    },
    handlePictureCardPreview(file) {
      this.showViewer = true;
      // this.dialogImageUrl = file.url;
      // this.dialogVisible = true;
    },
    handleDownload(file) {
      console.log(file);
    },
    beforeAvatarUpload(file) {
      console.log(file, "file");
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 20;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 20MB!");
      }
      return isJPG && isLt2M;
    },
  },
};
</script>
